
import * as actionTypes from '../constants/index';

import axios from 'axios'




//初始化数据
export const inforesult = (data,total) => {
  return  {type:actionTypes.GET_TAG_LIST,payload:data,total:total}
}
//loading
export const load=(data)=>{
  return {type:actionTypes.LOADING,loading:data}
}
// export const changepos=(data)=>{
//   return {type:actionTypes.NEGATIVE,positiveOrNegative:data}
// }
export const changepos=(data,k)=>{
  let ch = [{'负面':2,'正面':1,'中性':0,'全部':-1},{'全国':2,'本地':1,'境外':3,'全部':-1},{'已阅':1,'未阅':0,'全部':-1},{'时间降序':'poTimedesc','时间升序':'poTimeasc','相关性排序':'_scoredesc'},{'合并':1,'展开':0}]
  //let obj= Object.assign(getin.initState)
  let isFirst=false;
  if(data==='全部'){
    isFirst=true
  }
  return {type:actionTypes.NEGATIVE,positiveOrNegative:ch[k][data],isFirst:isFirst}
}
export const changeSco=(data,k)=>{
  let ch = [{'负面':2,'正面':1,'中性':0,'全部':-1},{'全国':2,'本地':1,'境外':3,'全部':-1},{'已阅':1,'未阅':0,'全部':-1},{'时间降序':'poTimedesc','时间升序':'poTimeasc','相关性排序':'_scoredesc'},{'合并':1,'展开':0}]
  let isFirst=false;
  if(data==='全部'){
    isFirst=true
  }
  return {type:actionTypes.SCOPE,scope:ch[k][data],isFirst:isFirst}
}

export const changesec=(data,k)=>{
  let ch = [{'负面':2,'正面':1,'中性':0,'全部':-1},{'全国':2,'本地':1,'境外':3,'全部':-1},{'已阅':1,'未阅':0,'全部':-1},{'时间降序':'poTimedesc','时间升序':'poTimeasc','相关性排序':'_scoredesc'},{'合并':1,'展开':0}]
  let sortType=ch[k][data].slice(0,5)
  let sortField=ch[k][data].slice(5)
  let isFirst=false
  if(data==='时间降序'){
    isFirst=true
  }
  return {type:actionTypes.ASDESC,sortType:sortType,sortField:sortField,isFirst:isFirst}
}
export const clearAll=()=>{
  return {type:actionTypes.CLEARAll,positiveOrNegative:-1,sortField:'poTime',sortType:'desc',scope:-1,isFirst:true}
}
export const pageSize=(data)=>{
  return {type:actionTypes.PAGESIZE,pagesize:data}
}
//数据类型筛选列表
export const typelist=(data)=>{
  return {type:actionTypes.SWITCH,switchlist:data}
}

//点击websitetype时的
export const changType=(data)=>{
  return  {type:actionTypes.WEBSITE,websiteType:data}
}

//改变筛选条件
export const tagchange=(data)=>{
  return  {type:actionTypes.REMOVEACTION,choselist:data}
}
//信息详情跳转
export const detail=(id,key)=>{
  return {type:actionTypes.DETAIL,id:id,titleContentPrint:key}
}
//检测修改
export const updata=(website,domian,type)=>{
  return {type:actionTypes.UPDATE,website:website,domian:domian,uptype:type}
}
//
export const websitetypelist=(
  startTime,
  endTime,
  positiveOrNegative,
  scope,
  sortType,
  sortField,
  isFirst,
  pagesize,
  websiteType,
  aggsField,
  searchType,
)=>{
  return async dispatch=>{
    let res =await axios.post('vrv-monitor-platform-web/data/queryTypes',{
      startTime:startTime,
      endTime:endTime,
      isFirst:isFirst,
      sortType:sortType,
      scope:scope,
      websiteType :websiteType,
      positiveOrNegative:positiveOrNegative,
      currentPage:1,
      limit:pagesize,
      sortField:sortField,
      abroad:-1,
      aggsField:aggsField,
      searchType:searchType
    })
    if(res.data.success){
      dispatch(typelist(res.data.data))
    }
    
  }
}

//导出所选的id
export const exportId=(deleteIds,)=>{
  return async dispatch=>{
    await window.open('vrv-monitor-platform-web/data/exportSelects?ids='+deleteIds)
}
}
//删除所选的id
export const deleteD=(dataid)=>{
  return {type:actionTypes.DELETE,id:dataid}
}

export const dataDelete=(
  deleteIds,
)=>{
  return async dispatch=>{
    let res = await axios.post('vrv-monitor-platform-web/data/delete',{
      deleteIds :deleteIds
    })
    if(res.data.success){
      dispatch(deleteD(deleteIds))
    }
  }
}


export const tabdata = (
  startTime,
  endTime,
  positiveOrNegative,
  scope,
  sortType,
  sortField,
  isFirst,
  pagesize,
  websiteType,
  currentPage,
  assignField,
  searchType,
)=>{
  return async dispatch=>{
    let res = await axios.post('vrv-monitor-platform-web/data/query',{
      startTime:startTime,
      endTime:endTime,
      isFirst:isFirst,
      sortType:sortType,
      scope:scope,
      websiteType :websiteType,
      positiveOrNegative:positiveOrNegative,
      currentPage:currentPage,
      limit:pagesize,
      sortField:sortField,
      abroad:-1,
      categoryId:'',
      searchType:searchType,
      topicId:"",
      aggsField:assignField
    })
    dispatch(load(true))
    if(res.data.success){
      localStorage.keywords=res.data.keywords;
      let value=res.data.data
      let data= (value[0] && value[0]['data']) ? value.map(d => ({doc_count: d.doc_count, ...d.data[0]})) : value
      dispatch(inforesult(data,res.data.totals))
      dispatch(load(false))
    }
  }
}
